const NewsModel = require("../../models/NewsModel")

const NewsService = {
    create: async (newsForm) => {
        const { title, content, category, cover, isPublish } = newsForm

        return NewsModel.create(
            {
                title,
                content,
                cover,
                isPublish,
                category,
                updateTime: new Date()
            }
        )
    },
    getList: async (id) => {
        if (id !== 'all') {
            console.log(id);

            return NewsModel.find({
                _id: id
            })
        }
        return NewsModel.find()
    },
    getStatus: async (id) => {
        const result = await NewsModel.findOne({
            _id: id
        })
        return result.isPublish
    },
    changeStatus: async (id) => {
        return NewsModel.updateOne({
            _id: id
        }, {
            isPublish: !(await NewsService.getStatus(id)),
        })
    },
    delete: async (id) => {
        return NewsModel.deleteOne({
            _id: id
        })
    },
    edit: async (newsForm) => {
        const { _id, title, content, category, cover, isPublish } = newsForm
        return NewsModel.updateOne({
            _id
        },
            {
                title,
                content,
                cover,
                isPublish,
                category,
                updateTime: new Date()
            }
        )
    }
}

module.exports = NewsService